package com.yiyuzi.admin.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.common.utils.ConvertUtils;
import com.yiyuzi.admin.role.RoleDao;
import com.yiyuzi.admin.role.domain.dto.RoleDTO;
import com.yiyuzi.admin.role.domain.dto.RolePrivilegeQueryDTO;
import com.yiyuzi.admin.role.domain.dto.RoleQueryDTO;
import com.yiyuzi.admin.role.domain.entity.RoleEntity;
import com.yiyuzi.admin.role.domain.entity.RolePrivilegeEntity;
import com.yiyuzi.admin.role.service.RolePrivilegeService;
import com.yiyuzi.admin.role.service.RoleService;
import com.yiyuzi.admin.user.domain.entity.UserRoleEntity;
import com.yiyuzi.admin.user.service.UserRoleService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author yiyuzi
 * @date 2021/5/8 10:08
 */

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RolePrivilegeService rolePrivilegeService;

    @Override
    public PageResultDTO<RoleDTO> findPage(RoleQueryDTO queryDTO) {
        List<RoleEntity> list = this.list(new QueryWrapper<RoleEntity>()
                .eq(queryDTO.getId() != null,"id", queryDTO.getId())
                .like(queryDTO.getRoleName() != null, "role_name", queryDTO.getRoleName())
                .eq(queryDTO.getFatherId() != null, "father_id", queryDTO.getFatherId()));
        return ConvertUtils.convertPageResult(list, RoleDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        if (!this.removeById(id)){
            throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
        }
        deleteRelation(id);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteList(List<Long> list) {
        for(Long id: list) {
            this.deleteById(id);
        }
        return true;
    }

    @Override
    public boolean export() throws IOException {
        HSSFWorkbook wk = new HSSFWorkbook(); // 创建表格
        HSSFSheet sk = wk.createSheet("商品表"); // 单元创建

        HSSFRow row1 = sk.createRow(0); // 创建第0行
        row1.createCell(0).setCellValue("id");
        row1.createCell(1).setCellValue("商品名称");
        row1.createCell(2).setCellValue("价格");

        HSSFRow row2 = sk.createRow(1); // 创建第1行
        row2.createCell(0).setCellValue("A01");
        row2.createCell(1).setCellValue("牛肉");
        row2.createCell(2).setCellValue(11);
        FileOutputStream file=new FileOutputStream("D:\\goods.xls"); //保存的路径
        wk.write(file); //保存
        file.flush();

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    void deleteRelation(Long id){
        List<UserRoleEntity> userRoleEntityList = userRoleService.list(new QueryWrapper<UserRoleEntity>()
                .eq(id != null,"role_id", id));
        for(UserRoleEntity entity : userRoleEntityList){
            if(!userRoleService.removeById(entity.getId())){
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
            }
        }

        List<RolePrivilegeEntity> rolePrivilegeDTOList = rolePrivilegeService.list(new QueryWrapper<RolePrivilegeEntity>()
                .eq(id != null,"role_id", id));
        for(RolePrivilegeEntity entity : rolePrivilegeDTOList){
            if(!rolePrivilegeService.removeById(entity.getId())){
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
            }
        }
    }
}
